package com.lh0811.forge.pserv.uacs.server.adapter.controller;

import com.github.pagehelper.PageInfo;
import com.lh0811.forge.dependency.basic_web.constant.AuthConstant;
import com.lh0811.forge.dependency.basic_web.response.ServerResponse;
import com.lh0811.forge.dependency.security.client.component.annotation.HasUrl;
import com.lh0811.forge.pserv.uacs.api.param.*;
import com.lh0811.forge.pserv.uacs.api.vo.UacsPlateUserQueryVo;
import com.lh0811.forge.pserv.uacs.api.vo.UacsTenantDepartmentPostQueryVo;
import com.lh0811.forge.pserv.uacs.server.service.PlateUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@Tag(name = "权限中心-平台用户管理")
@RestController
@RequestMapping("/forge/plate/v1/uacs/palte_user")
public class OpmsPlateUserController {


    @Resource
    private PlateUserService plateUserService;


    // 平台用户管理查询信息列表（分页）
    @HasUrl
    @GetMapping("/query/page")
    @Operation(summary = "平台用户管理查询信息列表（分页）", parameters = {
            @Parameter(in = ParameterIn.HEADER, name = AuthConstant.TokenKey, description = "用户Token")
    })
    public ServerResponse<PageInfo<UacsPlateUserQueryVo>> plateUserQueryPage(@ParameterObject @Valid UacsPlateUserQueryPageParam param) throws Exception {
        return ServerResponse.createBySuccess("查询成功", plateUserService.plateUserQueryPage(param));
    }

    // 平台用户管理查询信息列表（不分页）
    @HasUrl
    @GetMapping("/query/list")
    @Operation(summary = "平台用户管理查询信息列表（不分页）", parameters = {
            @Parameter(in = ParameterIn.HEADER, name = AuthConstant.TokenKey, description = "用户Token")
    })
    public ServerResponse<List<UacsPlateUserQueryVo>> plateUserQueryList(@ParameterObject @Valid UacsPlateUserQueryListParam param) throws Exception {
        return ServerResponse.createBySuccess("查询成功", plateUserService.plateUserQueryList(param));
    }

    // 平台用户创建
    @HasUrl
    @PutMapping("/create")
    @Operation(summary = "平台用户创建", parameters = {
            @Parameter(in = ParameterIn.HEADER, name = AuthConstant.TokenKey, description = "用户Token")
    })
    public ServerResponse plateUserCreate(@RequestBody @Valid UacsPlateUserCreateParam param) throws Exception {
        plateUserService.plateUserCreate(param);
        return ServerResponse.createBySuccess();
    }

    // 平台用户更新
    @HasUrl
    @PostMapping("/update")
    @Operation(summary = "平台用户更新", parameters = {
            @Parameter(in = ParameterIn.HEADER, name = AuthConstant.TokenKey, description = "用户Token")
    })
    public ServerResponse plateUserUpdate(@RequestBody @Valid UacsPlateUserUpdateParam param) throws Exception {
        plateUserService.plateUserUpdate(param);
        return ServerResponse.createBySuccess();
    }

    // 平台用户删除
    @HasUrl
    @DeleteMapping("/delete/{id}")
    @Operation(summary = "平台用户删除", parameters = {
            @Parameter(in = ParameterIn.HEADER, name = AuthConstant.TokenKey, description = "用户Token")
    })
    public ServerResponse plateUserDelete(@PathVariable(value = "id") Long id) throws Exception {
        plateUserService.plateUserDelete(id);
        return ServerResponse.createBySuccess();
    }

    // 保存用户岗位关系
    @HasUrl
    @PutMapping("/post_ref/save")
    @Operation(summary = "保存用户岗位关系", parameters = {
            @Parameter(in = ParameterIn.HEADER, name = AuthConstant.TokenKey, description = "用户Token")
    })
    public ServerResponse plateUserTenantPostRefSave(@RequestBody @Valid UacsPlateUserPostRefSaveParam param) throws Exception {
        plateUserService.plateUserTenantPostRefSave(param);
        return ServerResponse.createBySuccess();
    }

    // 保存用户岗位关系
    @HasUrl
    @GetMapping("/building/post/{userId}")
    @Operation(summary = "获取用户绑定的岗位", parameters = {
            @Parameter(in = ParameterIn.HEADER, name = AuthConstant.TokenKey, description = "用户Token")
    })
    public ServerResponse<List<UacsTenantDepartmentPostQueryVo>> getUserBuildingPostList(@PathVariable(value = "userId") Long userId) throws Exception {
        return ServerResponse.createBySuccess("获取成功", plateUserService.getUserBuildingPostList(userId));
    }


}
